#include <ctime>
#include "sound.h"
using std::string;

SDLSound &SDLSound::getInstance()
{
	static SDLSound instance;
	return instance;
}

SDLSound::~SDLSound() {
  std::cout << "Cleaning up sounds ..." << std::endl;
  std::clock_t start = std::clock();
  Mix_HaltMusic();
  Mix_FreeMusic(music);
  for (unsigned int i = 0; i < sounds.size(); ++i) {
    Mix_FreeChunk(sounds[i]);
  }
  Mix_CloseAudio();
  std::clock_t duration = std::clock() - start;
  std::cout << "Clean up took " << duration << " ticks\n";
}

SDLSound::SDLSound() : 
  musicVolume(MIX_MAX_VOLUME/4), 
  currentSound(-1), 
  music(NULL),
  audioRate(22050), 
  audioChannels(2), 
  audioBuffers(4096),
  sounds(),
  channels(),
  volumes() 
{
  if(Mix_OpenAudio(audioRate, MIX_DEFAULT_FORMAT, audioChannels, 
                   audioBuffers)){
    throw string("Unable to open audio!");
  }
  //music = Mix_LoadMUS("sounds/Larc.wav"); //background music
  // Need to install midi to play the following:
  //music = Mix_LoadMUS("sounds/ballad2.mid");
  //if (!music) throw string("Couldn't load 100Years.mp3")+Mix_GetError();

  //startMusic();
  sounds.push_back( Mix_LoadWAV("sounds/26DKONG1.WAV") ); //jump sound
  volumes.push_back(20);
  sounds.push_back( Mix_LoadWAV("sounds/26FLASHG1.WAV") );//shoot sound
  volumes.push_back(20);
  sounds.push_back( Mix_LoadWAV("sounds/dp_galaga88_bonus.wav") );  //item pickup
  volumes.push_back(20);
  sounds.push_back( Mix_LoadWAV("sounds/26PCOOKER2.WAV") ); //item respawn
  volumes.push_back(20);
  sounds.push_back( Mix_LoadWAV("sounds/26SINVADE3.WAV") ); //explosion/collision
  volumes.push_back(20);
  sounds.push_back( Mix_LoadWAV("sounds/mrdo_sta.wav") ); //start of game sound
  volumes.push_back(20);
  sounds.push_back( Mix_LoadWAV("sounds/Intro.wav") );
  volumes.push_back(20);
  for (unsigned int i = 0; i < sounds.size(); ++i) channels.push_back(i);
}

void SDLSound::toggleMusic() {
  if( Mix_PausedMusic() ) { 
    Mix_ResumeMusic(); 
  } 
  else { 
    Mix_PauseMusic(); 
  } 
}

void SDLSound::operator[](int index) {
  if (currentSound >= 0) Mix_HaltChannel(currentSound);
  currentSound = index;
  Mix_VolumeChunk(sounds[index], volumes[index]);
  channels[index] = Mix_PlayChannel(channels[index], sounds[index], 0);
}

SDLSound& SDLSound::operator++(){
	musicVolume += 10;
	if(musicVolume > MIX_MAX_VOLUME )
		musicVolume = MIX_MAX_VOLUME;
	Mix_VolumeMusic(musicVolume);
	std::cout << musicVolume << std::endl;
	return *this;
}


SDLSound& SDLSound::operator--(){
	musicVolume -= 10;
	if(musicVolume < 0 )
		musicVolume = 0;
	Mix_VolumeMusic(musicVolume);
	std::cout << musicVolume << std::endl;
	return *this;
}

void SDLSound::playSound(int index) {
  if (currentSound >= 0) Mix_HaltChannel(currentSound);
  currentSound = index;
  Mix_VolumeChunk(sounds[index], volumes[index]);
  channels[index] = Mix_PlayChannel(channels[index], sounds[index], 0);
}

void SDLSound::startMusic() {
  Mix_VolumeMusic(musicVolume);
  Mix_PlayMusic(music, -1);
}

void SDLSound::stopMusic() {
  Mix_HaltMusic();
  Mix_FreeMusic(music);
}

void SDLSound::setMusic(std::string music_path) {
	stopMusic();

	music = Mix_LoadMUS(music_path.c_str());
	if (!music) throw string("Couldn't load music ")+Mix_GetError();

	startMusic();
}
